package com;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author 罗俊华
 * @date 2021/9/30 - 5:48 下午
 */
@Slf4j
public class MyFuture<V> implements Configurable<V> {

    private volatile V result;

    private volatile boolean done;

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return this.done;
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {

        synchronized (this){
            while (!this.isDone()){
                wait();
            }
            return result;
        }

    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return null;
    }

    @Override
    public void setResult(V result) {

        synchronized (this){
            this.result = result;
            this.done = true;
            notify();
        }


    }
}
